import java.io.*;

public class KeaneFitnessFunction extends FitnessFunction
{
  public KeaneFitnessFunction()
  {
    super.name = "Keane (1994) Maximization Problem";
  }
  
  public void doRawFitness(Chromo c)
  {
    FloatChromo c2 = (FloatChromo) c;
    c2.rawFitness = 0.0;
    
    if (((FloatChromo)c).isFeasibleSolution())
    {
      double sum1 = 0.0, product1 = 1.0, sum2 = 0.0;
      double cos, cos2, x;
      
      for (int i = 0; i < c2.getValueVector().length; i++)
      {
        x = c2.getValueVector()[i];
        cos = Math.cos(x);
        cos2 = cos * cos;
        
        sum1 += cos2 * cos2;
        product1 *= cos2;
        sum2 += (i + 1) * x * x;
      }
      
      c2.rawFitness = Math.abs((sum1 - 2 * product1) / Math.sqrt(sum2));
      //System.out.println("FITNESS: " + c2.rawFitness);
    }
    
    
    // else fitness remains at 0.
  }
  
  public void doPrintGenes(Chromo x, FileWriter output) throws IOException
  {
    output.write("   RawFitness: " + x.rawFitness + "     <");
    
    for (int i = 0; i < Parameters.numGenes - 1; i++)
      output.write("" + ((FloatChromo)x).getValueVector()[i] + ", ");
    
    output.write("" + ((FloatChromo)x).getValueVector()[Parameters.numGenes - 1] + ">\n\n");
    return;
  }
}